package com.ywu.data.source.standard.service;

import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.gson.GsonUtils;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import com.ywu.data.source.standard.data.YwuDataExecRepository;
import com.ywu.data.source.standard.data.YwuDataExecVersionRepository;
import com.ywu.data.source.standard.dto.YwuDataConditionDto;
import com.ywu.data.source.standard.dto.YwuDataExecVersionDto;
import com.ywu.data.source.standard.entity.YwuDataExecEntity;
import com.ywu.data.source.standard.entity.YwuDataExecVersionEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName YwuDataExecVersionService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/10/31 21:12
 * @Version 1.0
 **/
@Service
public class YwuDataExecVersionService {
    @Autowired
    private YwuDataExecVersionRepository repository;

    @Autowired
    private YwuDataExecRepository ywuDataExecRepository;

    /**
    * @Author GroundDemo
    * @Description 更新草稿版本数据
    * @Date 21:25 2024/10/31
    * @Param [ywuDataExecEntity]
    * @return void
    **/
    public void updateVersionInfo(YwuDataExecEntity ywuDataExecEntity) {
        // 删除草稿版本的数据
        YwuDataExecVersionEntity deleteEntity = new YwuDataExecVersionEntity();
        deleteEntity.setResourceId(ywuDataExecEntity.getId());
        deleteEntity.setVersion(0);
        repository.deleteDataExecByVersion(deleteEntity);
        // 构建入参
        YwuDataExecVersionEntity ywuDataExecVersionEntity = new YwuDataExecVersionEntity();
        BeanUtils.copyProperties(ywuDataExecEntity, ywuDataExecVersionEntity);
        ywuDataExecVersionEntity.setVersion(0);
        ywuDataExecVersionEntity.setId(TimerIdGenerateUtil.nextId());
        ywuDataExecVersionEntity.setResourceId(ywuDataExecEntity.getId());
        repository.addVersionInfo(ywuDataExecVersionEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 根据resourceId删除所有的历史版本
    * @Date 21:43 2024/10/31
    * @Param [id]
    * @return void
    **/
    public void deleteAllVersionByResourceId(String id) {
        repository.deleteAllVersionByResourceId(id);
    }

    /**
    * @Author GroundDemo
    * @Description 发布草稿版本的数据
    * @Date 21:51 2024/10/31
    * @Param [queryRes]
    * @return void
    **/
    public void publishVersionInfo(YwuDataExecEntity queryRes) {
        // 草稿版本的数据拷贝到数据表中
        YwuDataExecVersionEntity search = new YwuDataExecVersionEntity();
        search.setVersion(0);
        search.setResourceId(queryRes.getId());
        YwuDataExecVersionEntity res = repository.getEntity(search);
        YwuDataExecEntity ywuDataExecEntity = new YwuDataExecEntity();
        BeanUtils.copyProperties(res, ywuDataExecEntity);
        ywuDataExecEntity.setId(queryRes.getId());
        ywuDataExecEntity.setStatus(YwuDataExecEntity.ONLINE_STATUS);
        ywuDataExecEntity.setCurrentVersion(queryRes.getCurrentVersion() + 1);
        ywuDataExecEntity.setHasNotReleased(false);
        // 修改表数据
        ywuDataExecRepository.updateDataExec(ywuDataExecEntity);
        // 修改version表中的版本
        YwuDataExecVersionEntity updateEntity = new YwuDataExecVersionEntity();
        updateEntity.setId(res.getId());
        updateEntity.setVersion(queryRes.getCurrentVersion() + 1);
        updateEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        repository.updateVersionInfo(updateEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 在version表中添加一条版本记录
    * @Date 21:52 2024/10/31
    * @Param [queryRes]
    * @return void
    **/
    public void addVersionInfo(YwuDataExecEntity queryRes) {
        // 构建参数
        YwuDataExecVersionEntity ywuDataExecVersionEntity = new YwuDataExecVersionEntity();
        BeanUtils.copyProperties(queryRes, ywuDataExecVersionEntity);
        ywuDataExecVersionEntity.setId(TimerIdGenerateUtil.nextId());
        ywuDataExecVersionEntity.createInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        ywuDataExecVersionEntity.setResourceId(queryRes.getId());
        ywuDataExecVersionEntity.setVersion(queryRes.getCurrentVersion() + 1);
        repository.addVersionInfo(ywuDataExecVersionEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 查询历史版本列表
    * @Date 11:21 2024/11/20
    * @Param [dataExecVersionDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryDataExecVersionList(YwuDataExecVersionDto dataExecVersionDto) {
        // 根据resourceId查询数据
        YwuDataExecVersionEntity search = new YwuDataExecVersionEntity();
        search.setResourceId(dataExecVersionDto.getResourceId());
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        List<YwuDataExecVersionEntity> queryList = repository.queryVersionList(search);
        queryList.forEach(res -> {
            String dataTableCode = res.getDataTableCode();
            String[] split = dataTableCode.split(",");
            res.setDataTableCodeList(Arrays.asList(split));
        });
        ArrayList<YwuDataExecVersionEntity> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(queryList)) {
            return CommonResult.success(result);
        }
        // 如果最后一条数据的版本为0，将其放置在首位
        if (queryList.get(queryList.size() - 1).getVersion() == 0) {
            YwuDataExecVersionEntity draftEntity = queryList.get(queryList.size() - 1);
            queryList.remove(queryList.size() - 1);
            result.add(draftEntity);
        }
        result.addAll(queryList);
        result.forEach(res -> {
            res.setExecFields(GsonUtils.deserializeList(res.getExecFieldsStr(), String.class));
            res.setExecCondition(GsonUtils.deserializeList(res.getExecConditionStr(), YwuDataConditionDto.class));
        });
        return CommonResult.success(result);
    }

    /**
    * @Author GroundDemo
    * @Description 删除草稿数据
    * @Date 11:22 2024/11/20
    * @Param [dataExecVersionDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteDraftVersion(YwuDataExecVersionDto dataExecVersionDto) {
        // resourceId删除草稿版本数据
        YwuDataExecVersionEntity ywuDataExecVersionEntity = new YwuDataExecVersionEntity();
        ywuDataExecVersionEntity.setVersion(0);
        ywuDataExecVersionEntity.setResourceId(dataExecVersionDto.getResourceId());
        repository.deleteByIdAndVersion(ywuDataExecVersionEntity);
        // 更改原始表中的hasNotReleased字段
        YwuDataExecEntity dataExecEntity = new YwuDataExecEntity();
        dataExecEntity.setId(dataExecVersionDto.getResourceId());
        dataExecEntity.setHasNotReleased(false);
        dataExecEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        ywuDataExecRepository.updateDataExec(dataExecEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 回退历史版本
    * @Date 11:22 2024/11/20
    * @Param [dataExecVersionDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> backDataExecVersion(YwuDataExecVersionDto dataExecVersionDto) {
        // 查询待回退的版本信息
        YwuDataExecVersionEntity search = new YwuDataExecVersionEntity();
        search.setResourceId(dataExecVersionDto.getResourceId());
        search.setVersion(dataExecVersionDto.getVersion());
        YwuDataExecVersionEntity queryEntity = repository.getEntity(search);
        // 添加草稿版本信息
        addDraftInfo(queryEntity);
        // 修改hasNotReleased状态
        YwuDataExecEntity ywuDataExecEntity = new YwuDataExecEntity();
        ywuDataExecEntity.setId(dataExecVersionDto.getResourceId());
        ywuDataExecEntity.setHasNotReleased(true);
        ywuDataExecEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        ywuDataExecRepository.updateDataExec(ywuDataExecEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 添加草稿信息
    * @Date 11:31 2024/11/20
    * @Param [queryEntity]
    * @return void
    **/
    private void addDraftInfo(YwuDataExecVersionEntity queryEntity) {
        YwuDataExecEntity ywuDataExecEntity = new YwuDataExecEntity();
        BeanUtils.copyProperties(queryEntity, ywuDataExecEntity);
        // 构建参数
        ywuDataExecEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        ywuDataExecEntity.setId(queryEntity.getResourceId());
        updateVersionInfo(ywuDataExecEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 根据resourceId批量查询草稿版本的数据
    * @Date 12:29 2024/11/20
    * @Param [resourceIds]
    * @return java.util.List<com.ywu.data.source.standard.entity.YwuDataExecVersionEntity>
    **/
    public List<YwuDataExecVersionEntity> queryDraftListByResourceIds(List<String> resourceIds) {
        return repository.queryDraftListByResourceIds(resourceIds);
    }

    /**
    * @Author GroundDemo
    * @Description 执行历史版本的数据
    * @Date 13:46 2024/11/20
    * @Param [ywuDataExecVersionDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public YwuDataExecVersionEntity getEntity(YwuDataExecVersionDto ywuDataExecVersionDto) {
        YwuDataExecVersionEntity search = new YwuDataExecVersionEntity();
        BeanUtils.copyProperties(ywuDataExecVersionDto, search);
        return repository.getEntity(search);
    }

    /**
    * @Author GroundDemo
    * @Description 查询历史版本详情数据
    * @Date 19:22 2025/9/8
    * @Param [dataExecVersionDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryVersionDetailInfo(YwuDataExecVersionDto dataExecVersionDto) {
        // 根据resourceId查询数据
        YwuDataExecVersionEntity search = new YwuDataExecVersionEntity();
        search.setResourceId(dataExecVersionDto.getResourceId());
        search.setVersion(dataExecVersionDto.getVersion());
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        YwuDataExecVersionEntity queryEntity = repository.getEntity(search);
        queryEntity.setDataTableCodeList(Arrays.asList(queryEntity.getDataTableCode().split(",")));
        return CommonResult.success(queryEntity);
    }
}
